package cardmodel

import (
	"context"
	"group-card/common/tool"
	"group-card/model/entity"
	"xorm.io/xorm"
)

var _ CardTraceabilityModel = (*customCardTraceabilityModel)(nil)

type (
	// CardTraceabilityModel is an interface to be customized, add more methods here,
	// and implement the added methods in customCardTraceabilityModel.
	CardTraceabilityModel interface {
		cardTraceabilityModel
		List(ctx context.Context, req *entity.CardTraceabilityReq) ([]*entity.CardTraceabilityResp, int64, error) // 分页查询
	}

	customCardTraceabilityModel struct {
		*defaultCardTraceabilityModel
	}
)

// NewCardTraceabilityModel returns a model for the database table.
func NewCardTraceabilityModel(conn *xorm.Engine) CardTraceabilityModel {
	return &customCardTraceabilityModel{
		defaultCardTraceabilityModel: newCardTraceabilityModel(conn),
	}
}

func (m *defaultCardTraceabilityModel) List(ctx context.Context, req *entity.CardTraceabilityReq) ([]*entity.CardTraceabilityResp, int64, error) {
	sql := m.conn.Table(m.table)

	if req.Columns != "" {
		sql.Select(req.Columns)
	}

	list := make([]*entity.CardTraceabilityResp, 0)
	count, err := sql.Limit(int(req.PageSize), int((req.PageNum-1)*req.PageSize)).FindAndCount(&list)
	if err != nil {
		tool.GLogLine("获取卡片溯源列表失败 defaultCardTraceabilityModel List:", err)
		return nil, 0, err
	}

	return list, count, err
}
